package projectx.transaction;

import org.apache.log4j.Logger;

import projectx.core.base.ProjectXSystemException;
import projectx.core.base.ProjectXUserException;

public abstract class Transaction implements ITransaction {

	// --------------------------------------------------------------------------
	// M E M B E R S
	//
	// --------------------------------------------------------------------------
	protected final String _funcId;
	protected final String _propPrefix;
	protected final long _maxInterval;
	protected final Logger _log;

	protected boolean _initialized;

	// --------------------------------------------------------------------------
	// C O N S T R U C T O R S
	//
	// --------------------------------------------------------------------------
	protected Transaction(String funcId) {

		this(funcId, 0);
	}

	protected Transaction(String funcId, long maxInterval) {

		_funcId = funcId;
		_propPrefix = funcId == null ? null : funcId;
		_maxInterval = maxInterval;
		_log = Logger.getLogger(getClass().getName());
	}

	// --------------------------------------------------------------------------
	// M E T H O D S
	//
	// --------------------------------------------------------------------------

	public String getFuncId() {
		return _funcId;
	}

	public String getPropPrefix() {
		return _propPrefix;
	}

	public long getMaxInterval() {
		return _maxInterval;
	}

	public abstract void initialize() throws ProjectXSystemException;

	public boolean isInitialized() {
		return _initialized;
	}

	public boolean keepResource(long interval) {
		return _maxInterval >= interval;
	}

	public abstract Object execute() throws ProjectXUserException,
			ProjectXSystemException;

	public abstract void commit() throws ProjectXSystemException;

	public abstract void rollback();

	public abstract void release();
}
